/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.futuremind.recyclerviewfastscroll.viewprovider;

import com.futuremind.recyclerviewfastscroll.Utils;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

import me.crosswall.lib.coverflow.ResourceTable;
/**
 * DefaultScrolledViewProvider
 *
 * @since 2021-03-26
 */
public class DefaultScrolledViewProvider extends ScrollerViewProvider {
    private static final String TAG = DefaultScrolledViewProvider.class.getSimpleName();
    /**
     * Component变量
     */
    protected Component bubble;
    /**
     * Component设置
     */
    protected Component handle;
    private final int mSum6 = 6;
    private final int mSum2 = 2;
    private final float mSum2f = 2f;
    private final int mSum8 = 8;
    private final int mSum180 = 180;
    private final int mSum20 = 20;
    private final int mSum0f = 0xf2e0e0e0;
    private ViewBehavior viewBehavior = null;

    @Override
    public Component provideHandleView(ComponentContainer container) {
        handle = new Component(getContext());
        int verticalInset = getScroller().isVertical() ? 0 : AttrHelper.vp2px(mSum6, getContext());
        int horizontalInset = !getScroller().isVertical() ? 0 : AttrHelper.vp2px(mSum8, getContext());
        ShapeElement shapeElement = new ShapeElement(getContext(), ResourceTable.Graphic_fastscroll__default_handle);

        Utils.setBackground(handle, shapeElement);
        int handleWidth = AttrHelper.vp2px(getScroller().isVertical() ? mSum6 : mSum180, getContext()); // 把长度转化为vp单位
        int handleHeight = AttrHelper.vp2px(getScroller().isVertical() ? mSum180 : mSum6, getContext());

        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(handleWidth, handleHeight);
        layoutConfig.setMargins(horizontalInset, verticalInset, horizontalInset, verticalInset);

        handle.setLayoutConfig(layoutConfig);
        return handle;
    }

    @Override
    public Component provideBubbleView(ComponentContainer container) {
        int radius = AttrHelper.vp2px(mSum20, getContext());
        float[] radii = new float[]{
                radius, radius,
                radius, radius,
                0, 0,
                radius, radius
        };
        ShapeElement shapeElement2 = new ShapeElement(getContext(), ResourceTable.Graphic_fastscroll__default_bubble);
        shapeElement2.setCornerRadiiArray(radii);
        shapeElement2.setStroke(mSum2, RgbColor.fromArgbInt(mSum0f));
        bubble = LayoutScatter.getInstance(getContext())
                .parse(ResourceTable.Layout_fastscroll_defalut_bubble, container, false);

        bubble.setBackground(shapeElement2);

        return bubble;
    }

    @Override
    public Text provideBubbleTextView() {
        return (Text) bubble;
    }

    @Override
    public int getBubbleOffset() {
        return (int) (getScroller().isVertical() ? ((float) handle.getHeight())
                / mSum2f - bubble.getHeight() : ((float) handle.getWidth()) / mSum2f - bubble.getWidth());
    }

    @Override
    protected ViewBehavior provideHandleBehavior() {
        return viewBehavior;
    }

    @Override
    protected ViewBehavior provideBubbleBehavior() {
        return new DefaultBubbleBehavior(new VisibilityAnimationManager.Builder(bubble)
                .withPivotX(1f).withPivotY(1f).build());
    }
}
